package cn.yinyu.queue.module.ecg.service.config;

import cn.yinyu.queue.module.ecg.Utils;
import cn.yinyu.queue.module.ecg.config.DynamicSchedulingConfig;
import cn.yinyu.queue.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.yinyu.queue.module.ecg.dal.mysql.queuesequence.QueueSequenceMapper;
import cn.yinyu.queue.module.ecg.service.queue.QueueService;
import cn.yinyu.queue.module.ecg.service.queuesequence.QueueSequenceService;
import cn.yinyu.queue.module.ecg.service.room.RoomService;
import cn.yinyu.queue.module.infra.api.config.ConfigApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import static cn.yinyu.queue.module.ecg.Constants.*;

@Service
@Validated
@Slf4j
public class EcgConfigServiceImpl implements EcgConfigService{

    @Resource
    private ConfigApi configApi;

    @Resource
    private RoomService roomService;

    @Resource
    private QueueService queueService;

    @Resource
    private QueueSequenceService queueSequenceService;

    List<LocalTime> timeslotList = null;
    Integer timeslotLength = 0;

    ScheduledTask startBizTask = null;
    ScheduledTask closeBizTask = null;
    ScheduledTask resetRoomTask = null;

    @Override
    public void resetScheduler() {
        ScheduledTaskRegistrar taskRegistrar = DynamicSchedulingConfig.static_scheduledTaskRegistrar;

        if (null != startBizTask) startBizTask.cancel();
        if (null != closeBizTask) closeBizTask.cancel();
        if (null != resetRoomTask) resetRoomTask.cancel();

        String strOpenCloseTime = configApi.getConfigValueByKey(ECG_OPENING_TIME_KEY);
        List<LocalTime> list = Utils.parseOpeningTime(strOpenCloseTime);
        LocalTime openingTime = list.get(0);
        LocalTime closeTime = list.get(1);
        String openCronExpression = String.format("0 %d %d  * * ?", openingTime.getMinute(), openingTime.getHour());
        String closeCronExpression = String.format("0 %d %d  * * ?", closeTime.getMinute(), closeTime.getHour());

        String strRoomResetTime = configApi.getConfigValueByKey(ECG_ROOM_RESET_TIME_KEY);
        LocalTime roomResetTime = Utils.parseTime(strRoomResetTime);
        String roomResetCronExpression = String.format("0 %d %d  * * ?", roomResetTime.getMinute(), roomResetTime.getHour());

        startBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Opening Task executed at: " + System.currentTimeMillis());
            queueService.startBiz();
        }, openCronExpression));

        closeBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Close Task executed at: " + System.currentTimeMillis());
            queueService.closeBiz();
        }, closeCronExpression));

        resetRoomTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Room Reset Task executed at: " + System.currentTimeMillis());
            roomService.resetRoom(false);
            queueSequenceService.resetQueueSequence();
        }, roomResetCronExpression));

        taskRegistrar.afterPropertiesSet();
    }

    @Override
    public void readTimeSlotConfig() {
        String strBookTimeslotLength = configApi.getConfigValueByKey(BOOK_TIMESLOT_LENGTH);
        timeslotLength = Integer.valueOf(strBookTimeslotLength);
        //String strBookTimeslotList = configApi.getConfigValueByKey(BOOK_TIMESLOT_LIST);
        //timeslotList = Utils.parseTimeSlotList(strBookTimeslotList, Integer.valueOf(strBookTimeslotLength));
    }

    @Override
    public List<LocalTime> listTimeslot() {
        return  timeslotList;
    }

    @Override
    public Integer getTimeslotLength() {
        return timeslotLength;
    }

}
